using Xunit;
using Param;
using System.Net;
using Exceptions;
using System;

namespace xUnitTest
{
    public class TestParams
    {
        [Fact]
        public void Test()
        {
            Params aParams;

            aParams = new Params();
            aParams.Parse(new string[0]);
            Assert.Equal(1, aParams.GetValue("-a", 1));
            Assert.Equal("abcd", aParams.GetValue("-B", "abcd"));
            Assert.Equal(IPAddress.Parse("0.0.0.0"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));

            aParams = new Params();
            aParams.Parse(new String[1] { "999" });
            Assert.Equal(999, aParams.GetValue("-a", 1));
            Assert.Equal("abcd", aParams.GetValue("-B", "abcd"));
            Assert.Equal(IPAddress.Parse("0.0.0.0"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));

            aParams = new Params();
            aParams.Parse("999".Split(' '));
            Assert.Equal("999", aParams.GetValue("-B", "abcd"));
            Assert.Equal(1, aParams.GetValue("-a", 1));
            Assert.Equal(IPAddress.Parse("0.0.0.0"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));

            aParams = new Params();
            aParams.Parse("255.255.255.255".Split(' '));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal("abcd", aParams.GetValue("-B", "abcd"));
            Assert.Equal(1, aParams.GetValue("-a", 1));

            aParams = new Params();
            aParams.Parse("255.255.255.255 999 abcdefg".Split(' '));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal(999, aParams.GetValue("-a", 1));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(999, aParams.GetValue("-a", 1));

            aParams = new Params();
            aParams.Parse("-cAT 255.255.255.255 -A 999 -B abcdefg".Split(' '));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal(999, aParams.GetValue("-a", 1));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(999, aParams.GetValue("-a", 1));

            aParams = new Params();
            aParams.Parse("-A 999 -cAT 255.255.255.255 -B abcdefg".Split(' '));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal(999, aParams.GetValue("-a", 1));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(999, aParams.GetValue("-a", 1));

            aParams = new Params();
            aParams.Parse("-B abcdefg -A 999 -cAT 255.255.255.255".Split(' '));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal(999, aParams.GetValue("-a", 1));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(999, aParams.GetValue("-a", 1));

            aParams = new Params();
            aParams.Parse("-B abcdefg -A 999 -cAT 255.255.255.255".Split(' '));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal(999, aParams.GetValue("-a", 1));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(999, aParams.GetValue("-a", 1));

            aParams = new Params();
            aParams.Parse("abcdefg -A 999 -cAT 255.255.255.255".Split(' '));
            Assert.Equal(999, aParams.GetValue("-a", 1));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(999, aParams.GetValue("-a", 1));

            aParams = new Params();
            aParams.Parse("abcdefg -A 999 -cAT 255.255.255.255".Split(' '));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(999, aParams.GetValue("-a", 1));
            Assert.Equal(999, aParams.GetValue("-a", 1));

            aParams = new Params();
            aParams.Parse("abcdefg -A 999 -cAT 255.255.255.255".Split(' '));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal(999, aParams.GetValue("-a", 1));
            Assert.Equal(999, aParams.GetValue("-a", 1));

            aParams = new Params();
            aParams.Parse("abcdefg 999 -cAT 255.255.255.255".Split(' '));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal(999, aParams.GetValue("-a", 1));
            Assert.Equal(999, aParams.GetValue("-a", 1));

            aParams = new Params();
            aParams.Parse("abcdefg -cAT 255.255.255.255 999".Split(' '));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal(999, aParams.GetValue("-a", 1));
            Assert.Equal(999, aParams.GetValue("-a", 1));

            aParams = new Params();
            aParams.Parse("abcdefg -A -cAT 255.255.255.255 999".Split(' '));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal("", aParams.GetValue("-a", "none"));

            aParams = new Params();
            aParams.Parse("abcdefg -A 888 -A 777 -cAT 255.255.255.255 999".Split(' '));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal(888, aParams.GetValue("-a", 1));

            aParams = new Params();
            aParams.Parse("abcdefg -A 888 -A 777 -cAT 255.255.255.255 999 -xx".Split(' '));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal(888, aParams.GetValue("-a", 1));
            Assert.Equal("", aParams.GetValue("-xx", "xyz"));

            aParams = new Params();
            aParams.Parse("abcdefg -A 888 -A 777 -cAT 255.255.255.255 999 -xx false".Split(' '));
            Assert.Equal("abcdefg", aParams.GetValue("-B", "abcd"));
            Assert.Equal(IPAddress.Parse("255.255.255.255"), aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")));
            Assert.Equal(888, aParams.GetValue("-a", 1));
            Assert.False(aParams.GetValue("-xx", false));

            aParams = new Params();
            aParams.Parse("-A xuv -cAT abc.789 -xx xxx".Split(' '));
            Assert.Throws<ExceptionStopApp>(new Action(() => { aParams.GetValue("-a", 1); }));
            Assert.Throws<ExceptionStopApp>(new Action(() => { aParams.GetValue("-Cat", IPAddress.Parse("0.0.0.0")); }));
            Assert.Throws<ExceptionStopApp>(new Action(() => { aParams.GetValue("-xx", false); }));
        }
    }
}